package com.ruoyi.common.utils;

import jodd.util.StringUtil;

/**
 * 计算两点间直线距离
 *
 * @Author: triones
 */
public class GetDistance {
    /**
     * //赤道半径(单位m)
     */
    private static double EARTH_RADIUS = 6371000;


    private static final double EARTH_RADIUS_A = 6378.137;

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     *
     * @param lon1 第一点的经度
     * @param lat1 第一点的维度
     * @param lon2 第二点的经度
     * @param lat2 第二点的维度
     * @return
     */
    public static double getDistance(double lon1, double lat1, double lon2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000 / 1000;
        return s;
    }
//
//
//    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
//        // 纬度
//        double lat1 = Math.toRadians(latitude1);
//        double lat2 = Math.toRadians(latitude2);
//        // 经度
//        double lng1 = Math.toRadians(longitude1);
//        double lng2 = Math.toRadians(longitude2);
//        // 纬度之差
//        double a = lat1 - lat2;
//        // 经度之差
//        double b = lng1 - lng2;
//        // 计算两点距离的公式
//        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
//                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
//        // 弧长乘地球半径, 返回单位: 千米
//        s =  s * EARTH_RADIUS_A;
//        return Math.round(s * 10000) / 10000;
//    }

    public static double getDistance(String originLon, String originLat, String destinationLon, String destinationLat) {
        if (StringUtil.isEmpty(originLon)) {
            return 0;
        }
        if (StringUtil.isEmpty(originLat)) {
            return 0;
        }
        if (StringUtil.isEmpty(destinationLon)) {
            return 0;
        }
        if (StringUtil.isEmpty(destinationLat)) {
            return 0;
        }

        double radLat1 = rad(Double.parseDouble(originLat));
        double radLat2 = rad(Double.parseDouble(destinationLat));
        double a = radLat1 - radLat2;
        double b = rad(Double.parseDouble(originLon)) - rad(Double.parseDouble(destinationLon));
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
            + Math.cos(radLat1) * Math.cos(radLat2)
            * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        // 保留两位小数
        s = Math.round(s * 100d) / 100d;
        s = s * 1000;
        return s;
    }


    public static void main(String[] args) {
        long raidus = 10000; //半径10km
        Double lon = 106.536627; //当前经度
        Double lat = 29.591484; //当前纬度
        Double lon1= 106.531344;
        Double lat1= 29.676162;
        double dist;
        dist= getDistance(lon, lat, lon1, lat1);
        System.out.println("1.两点相距：" + dist + " 米");
        int intValue = (int) Math.round(dist);
        System.out.println("intValue = " + intValue);
    }
}
